using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.IO.Compression;
using System.Text;
using NUnit.Framework;
using Universe.JRun;

namespace Universe.CrossIO.Test
{
    [TestFixture]
    public class TestGZip
    {


        private JRunner GetJUnPacker()
        {
            return new JRunner("cldctest.MainGZip_UnPack", MyEnvironment.ClassPath);
        }

        private JRunner GetJPacker()
        {
            return new JRunner("cldctest.MainGZip_Pack", MyEnvironment.ClassPath);
        }

        [Test]
        [Ignore("Unpack stream of 2Gb size")]
        public void TestJUnPack2Gb()
        {
            JRunner jc = GetJUnPacker();
            using (MyTemp tmp = new MyTemp())
            {
                ExecUnPack(0x7FFFFFFF, tmp, jc);
            }
        }

        [Test]
        public void TestJUnPack()
        {
            JRunner jc = GetJUnPacker();
            using (MyTemp tmp = new MyTemp())
            {
                foreach (int size in MyEnvironment.Sizes)
                    ExecUnPack(size, tmp, jc);
            }
        }


        [Test]
        public void TestJPack()
        {

            JRunner jc = GetJPacker();
            using (MyTemp tmp = new MyTemp())
            {
                foreach (int size in MyEnvironment.Sizes)
                    ExecPack(size, tmp, jc);
            }
        }

        [Test]
        [Ignore("Pack stream of 2Gb size")]
        public void TestJPack2Gb()
        {
            JRunner jc = GetJPacker();
            using (MyTemp tmp = new MyTemp())
            {
                ExecPack(0x7FFFFFFF, tmp, jc);
            }
        }

        private void ExecUnPack(int size, MyTemp tmp, JRunner jc)
        {
            for(int sw = 0; sw < MyEnvironment.SourceWriters.Length; sw++)
            {
                string fileName = MyEnvironment.SourceWriters[sw].Method.Name + "_" + size + ".source";
                using (FileStream fs = new FileStream(tmp[fileName], FileMode.Create, FileAccess.Write, FileShare.Read))
                {
                    MyEnvironment.SourceWriters[sw](fs, size);
                }

                string nameNZip = Path.ChangeExtension(fileName, ".nzip");
                using (FileStream streamSource = new FileStream(tmp[fileName], FileMode.Open, FileAccess.Read, FileShare.Read, 32768))
                using(FileStream streamTarget = new FileStream(tmp[nameNZip], FileMode.Create, FileAccess.Write, FileShare.Read))
                using(GZipStream packer = new GZipStream(streamTarget, CompressionMode.Compress))
                using(BufferedStream bPacker = new BufferedStream(packer, 8192))
                {
                    MyEnvironment.Copy(streamSource, bPacker);
                }

                string nameRePlain = Path.ChangeExtension(fileName, ".re");

                jc.Run(tmp[nameNZip], tmp[nameRePlain]);

                bool areEqual = MyEnvironment.ComparePlainAndPlain(tmp[fileName], tmp[nameRePlain]);
                Assert.IsTrue(areEqual, "UnPack on J2ME of " + nameNZip);

                Trace.WriteLine("NET Pack " + size + ": " + fileName + " is " + new FileInfo(tmp[nameNZip]).Length);

            }

        }

        private void ExecPack(int size, MyTemp tmp, JRunner jc)
        {
            for (int sw = 0; sw < MyEnvironment.SourceWriters.Length; sw++)
            {
                string fileName = MyEnvironment.SourceWriters[sw].Method.Name + "_" + size + ".source";
                using (FileStream fs = new FileStream(tmp[fileName], FileMode.Create, FileAccess.Write, FileShare.Read))
                {
                    MyEnvironment.SourceWriters[sw](fs, size);
                }

                string nameJZip = Path.ChangeExtension(fileName, ".jzip");
                JResponse res = jc.Run(tmp[fileName], tmp[nameJZip]);
                Assert.AreEqual("", res.Errors, "Exec GZipOutputStream");

                bool areEquls = MyEnvironment.ComparePlainAndGZipped(tmp[fileName], tmp[nameJZip]);
                Assert.IsTrue(areEquls, "Packed on J2ME is fine for " + fileName);
                Trace.WriteLine("Java Pack " + size + ": " + fileName + " is " + new FileInfo(tmp[nameJZip]).Length);
            }
        }
    }

}
